package ca.ubc.eece310.project;

/**
 * EECE 310 Term Project Fall 2009
 * @author Matthew Fong, Herman Lee, Scott Pearson
 */

public class MyPacket {

	/**
	 * OVERVIEW: A simple packet containing the headers of a PcapPacket
	 * Rep-invariant: source != null, destination != null
	 * AF(c): c.source, c.destination, c.sourcePort, c.destinationPort, c.size;
	 */
	private String source, destination;
	private int sourcePort, destinationPort;
	private double size;

	// CONSTRUCTOR
	public MyPacket(String source, String destination, int size,
			int sourcePort, int destinationPort) {
		/**
		 * REQUIRES: source != null, destination != null EFFECTS: Creates a
		 * packet with such information
		 */
		this.source = source;
		this.destination = destination;
		this.size = size;
		this.sourcePort = sourcePort;
		this.destinationPort = destinationPort;
	}

	public String getSource() {
		/**
		 * EFFECTS: Returns the source IP of the packet
		 */
		return source;
	}

	public String getDestination() {
		/**
		 * EFFECTS: Returns the destination IP of the packet
		 */
		return destination;
	}

	public int getSourcePort() {
		/**
		 * EFFECTS: Returns the source port of the packet
		 */
		return sourcePort;
	}

	public int getDestinationPort() {
		/**
		 * EFECTS: Returns the destination port of the packet
		 */
		return destinationPort;
	}

	public double getSize() {
		/**
		 * EFFECTS: Returns the size of the packet
		 */
		return size;
	}


	public boolean equals(MyPacket p) {
		/**
		 * EFFECTS: Returns true if this is the same packet as p false if it
		 * isn't
		 */
		if (source.equals(p.getSource())
				&& destination.equals(p.getDestination())
				&& sourcePort == p.getSourcePort()
				&& destinationPort == p.getDestinationPort()
				&& size == p.getSize())
			return true;
		return false;
	}

	public int hashCode() {
		/**
		 * EFFECTS: Creates a hashCode for this packet
		 */
		return 37 * (sourcePort - destinationPort) * (int) Math.round(size);
	}
	
	public boolean repOk() {
		/**
		 * EFFECTS: Returns true if the rep invariant holds for this
		 * 			else returns false
		 */
		if(source!=null && destination!=null)
			return true;
		return false;
	}
	
	public String toString() {
		/**
		 * EFFECTS: Returns a String representation
		 */
		return "Source: " + source + ":" + sourcePort + 
				"\nDestination: " + destination + ":" + destinationPort + 
				"\nSize: " + size;
	}
}
